#include "sqlconn.h"

sqlConn::sqlConn(QString path)
{
    sqlsetpath = path;
    QSettings *setting = new QSettings(path,QSettings::IniFormat);

    hostname = setting->value("database/hostname").toString();
    dbname = setting->value("database/databasename").toString();
    usrname = setting->value("database/username").toString();
    dbpsd = setting->value("database/password").toString();

    delete setting;

    if(QSqlDatabase::contains("qt_sql_default_connection"))
      db = QSqlDatabase::database("qt_sql_default_connection");
    else
      db = QSqlDatabase::addDatabase("QMYSQL");

    db.setHostName(hostname);

    db.setDatabaseName(dbname);

    db.setUserName(usrname);

    db.setPassword(dbpsd);

}

void sqlConn::connect()
{
    if (!db.open()) {

        qDebug() << "Failed to connect to root mysql admin";
        exit(1);
    }
}

void sqlConn::close()
{
    db.close();
    return ;
}

QSqlQuery *sqlConn::queryData(QString sqlstr)
{
    QSqlQuery *q = new QSqlQuery();
    q->exec(sqlstr);
    return q;
}

void sqlConn::saveData(QString sqlstr)
{
    QSqlQuery query ;
    query.prepare(sqlstr);
    query.exec();
    return ;
}


